<%
dim treelistcontrol_js
dim treelistcontrol_uid
treelistcontrol_uid = 0

class treelistcontrol_node
	public id
	public parentid
	public columntext
	public icon
	public expanded
	public children
	private sub class_initialize
		set children = createobject("scripting.dictionary")
	end sub
end class

class treelistcontrol
private test

	private nodes, nodeindex
	private ncolumns
	private columntext()
	private stack(), stacksize
	
	private icon_collapse_end
	private icon_collapse_more
	private icon_expand_end
	private icon_expand_more
	private icon_connect_end
	private icon_connect_more
	private icon_connect_bridge
	private icon_empty

	private rowcolor1, rowcolor2, currentrow
	private selectednode
	
	public tablewidth
	public allowrowselect
	public showborder
	public pathtoicons
	public showdividers
	
	private sub class_initialize
		treelistcontrol_uid = treelistcontrol_uid + 1
		tablewidth = "100%"
		showborder = false
		ncolumns = 0
		set nodes = createobject("scripting.dictionary")
		set nodeindex = createobject("scripting.dictionary")
		redim stack(500)
		stacksize = 0

		icon_collapse_end = "icon_collapse_end.gif"
		icon_collapse_more = "icon_collapse_more.gif"
		icon_expand_end = "icon_expand_end.gif"
		icon_expand_more = "icon_expand_more.gif"
		icon_connect_end = "icon_connect_end.gif"
		icon_connect_more = "icon_connect_more.gif"
		icon_connect_bridge = "icon_connect_bridge.gif"
		icon_empty = "icon_empty.gif"
		pathtoicons = pathtoroot & "web_api/userinterface/treelistcontrol/"
		
		rowcolor1 = "#F7F8FA"
		rowcolor2 = "#FFFFFF"
		allowrowselect = false
		currentrow = 1
		redim columntext(500,2)
	end sub
	
	public sub addcolumn(text, size, align)
		columntext(ncolumns,0) = text
		columntext(ncolumns,1) = size
		columntext(ncolumns,2) = align
		ncolumns = ncolumns + 1
	end sub
	
	public sub revealnode(id)
		dim parentid
		redim stack(500)
		stacksize = 0
		parentid = nodeindex(id)
		if parentid = 0 then exit sub
		stack(0) = parentid
		stacksize = 1
		while not nodeindex(stack(stacksize-1)) = 0
			stack(stacksize) = nodeindex(stack(stacksize-1))
			stacksize = stacksize + 1
		wend
		dim i, obj
		i = stacksize-1
		set obj = nodes(cstr(stack(i)))
		obj.expanded = true
		while not obj.id = parentid
			i = i - 1
			set obj = obj.children(cstr(stack(i)))
			obj.expanded = true
		wend
		stacksize = 0
	end sub
	
	public sub selectnode(id)
		selectednode = id
	end sub
	
	public sub addnode(byval id, byval parentid, icon, expand, columntext) 'columntext must be passed as an array containing labels for every column
		nodeindex.add id, parentid
		dim inode
		set inode = new treelistcontrol_node
		inode.id = id
		inode.parentid = parentid
		inode.icon = icon
		inode.expanded = expand
		inode.columntext = columntext
		if parentid = 0 then
			nodes.add cstr(id), inode
		else
			stack(0) = parentid
			stacksize = 1
			while not nodeindex(stack(stacksize-1)) = 0
				stack(stacksize) = nodeindex(stack(stacksize-1))
				stacksize = stacksize + 1
			wend
			dim i, obj
			i = stacksize-1
			set obj = nodes(cstr(stack(i)))
			while not obj.id = parentid
				i = i - 1
				set obj = obj.children(cstr(stack(i)))
			wend
			stacksize = 0
			obj.children.add cstr(id), inode
		end if
	end sub
	
	public function value
		dim item, str, i, tdstyle
		str = str & "<script language=""javascript"">" & vbcrlf
		if not treelistcontrol_js then
			str = str & vbcrlf & loadjavascript & vbcrlf
			treelistcontrol_js = true
		end if
		str = str & "treelistctrl[" & treelistcontrol_uid & "] = null;" & vbcrlf
		str = str & "</script>" & vbcrlf
		str = str & "<table onmouseover=""document.body.style.cursor='default';"" onmouseout=""document.body.style.cursor='';"" border=""0"" cellpadding=""0"" cellspacing=""0"""
		if showborder then str = str & " style=""border-top:1px solid buttonshadow; border-left:1px solid buttonshadow; border-right:1px solid buttonhighlight; border-bottom:1px solid buttonhighlight"""
		str = str & " width=""" & tablewidth & """>"
		str = str & "<thead style=""background-color:buttonface;""><tr>" & vbcrlf
		for i = 0 to ncolumns - 1
			if i = ncolumns - 1 then
				tdstyle = "border:1px solid #404040;"
			else
				tdstyle = "border-top:1px solid #404040; border-left:1px solid #404040; border-bottom:1px solid #404040;"
			end if
			str = str & "<td style=""" & tdstyle & """ nowrap"
			if len(columntext(i,1)) > 0 then str = str & " width=""" & columntext(i,1) & """"
			str = str & ">" & vbcrlf & "<table border=""0"" cellpadding=""0"" cellspacing=""0"" width=""100%""" &_
						" height=""100%"">" & vbcrlf & "<tr><td style=""border:1px outset; padding-left:3px; padding-right:3px; font-family: tahoma; font-size: 8pt"" nowrap align=""" &_
						columntext(i,2) & """>" & columntext(i,0) & "</td></tr>" & vbcrlf & "</table></td>" & vbcrlf
		next
		str = str & "</tr></thead><tbody style=""text-indent: 4px; font-family: tahoma; font-size: 8pt"">"
		for each item in nodes
			str = str & getbranch(nodes(item))
		next
		str = str & "</tbody></table>"
		value = str
	end function
	
	private function loadjavascript
		dim filename
		filename = server.mappath(pathtoicons & "treelistcontrol.js")
		if true then 'application(filename) & "" = "" then
			dim fso, file, txt
			set fso = createobject("Scripting.FileSystemObject")
			on error resume next
			set file = fso.opentextfile(filename,1,false)
			if err then
				txt = filename & " was not found."
			else
				txt = file.readall
			end if
			application.lock
			application(filename) = txt
			application.unlock
			file.close
			set file = nothing
			set fso = nothing
		end if
		loadjavascript = application(filename)
	end function
	
	private function getbranch(obj)
		dim item, str
		str = str & makerow(obj)
		for each item in obj.children
			str = str & getbranch(obj.children(item))
		next
		getbranch = str
	end function
	
	private function makerow(obj)
		dim o, keys, imgs
		o = obj.id
		stacksize = 0
		while o > 0
			stack(stacksize) = o
			stacksize = stacksize + 1
			o = nodeindex(o)
		wend
		dim str, i, cobj, cobjp, expand, visible, isEnd
		str = str & "treelistcontrolrow" & treelistcontrol_uid
		for i = stacksize-1 to 0 step -1
			if i=stacksize-1 then
				'set cobj = nodes(cstr(stack(i)))
				set cobj = nodes
				keys = nodes.keys
				visible = true
			else
				set cobjp = cobj
				set cobj = cobjp(cstr(stack(i+1))).children
				keys = cobjp(cstr(stack(i+1))).children.keys
				if visible then visible = cobjp(cstr(stack(i+1))).expanded
			end if
			if cobj(cstr(stack(i))).id = cint(keys(ubound(keys))) then isEnd = true else isEnd = false
			if isend then
			'	stopapp obj.id
			end if
			imgs = imgs & "<img align=""absmiddle"" width=""16"" height=""16"" src=""" & pathtoicons
			if i > 0 then
				'do parent connector icon
				if isEnd then
					imgs = imgs & icon_empty
				else
					imgs = imgs & icon_connect_bridge
				end if
			else
				'do my connector icon
				if obj.children.count > 0 then
					if obj.expanded then
						if isEnd then
							imgs = imgs & icon_collapse_end
						else
							imgs = imgs & icon_collapse_more
						end if
					else
						if isEnd then
							imgs = imgs & icon_expand_end
						else
							imgs = imgs & icon_expand_more
						end if
					end if
					imgs = imgs & """ onclick=""treelistcontrol_showhide(this," & treelistcontrol_uid & ");"
				else
					if isEnd then
						imgs = imgs & icon_connect_end
					else
						imgs = imgs & icon_connect_more
					end if
				end if
			end if
			imgs = imgs & """>"
			str = str & "_" & stack(i)
		next
		str = "<tr id=""" & str & """"
		if not visible or cstr(obj.id) = cstr(selectednode) then
			str = str & " style="""
			if not visible then
				str = str & "display:none;"
			else
				str = str & "color:#FFFFFF; background-color:#000066"
			end if
			str = str & """"
		end if
		if allowrowselect then str = str & " onclick=""treelistcontrol_rowselect(this);"""
		str = str & " bgcolor="""
		if visible then
			if currentrow = 1 then currentrow = 2 else currentrow = 1
			if currentrow = 1 then str = str & rowcolor1 else str = str & rowcolor2
		end if
		str = str & """><td nowrap>"
		str = str & imgs & "<img src=""" & obj.icon & """ width=""16"" height=""16"" align=""absmiddle""> " & obj.columntext(0) & "</td>"
		for i = 1 to ubound(obj.columntext)
			str = str & "<td nowrap"
			if showdividers then str = str & " style=""border-left: 1px solid #F2F3F5; padding-left: 4px;"""
			if len(columntext(i,1))>0 then str = str & " width=""" & columntext(i,1) & """"
			str = str & " align=""" & columntext(i,2) & """>" & obj.columntext(i) & "</td>"
		next
		str = str & "</tr>" & vbcrlf
		makerow = str
	end function
end class
%>